const router = require("koa-router")();
const db = require("../database/db-mysql")
const menuController = require("../controller/menus")

router.prefix("/menus");

router.get("/all", menuController.findAll)

// 添加一级菜单
router.post("/addFirstLevel", menuController.addFirstLevel)

// 添加子菜单
router.post("/addChild", menuController.add)

// 修改菜单
router.put("/editMenu", menuController.update)

// 删除菜单
router.delete("/delete", menuController.del)

// 根据id查询菜单名称
router.get("/getMenunameById", async (ctx, next) => {
    let { id } = ctx.request.query;
    let res = await db.query("select * from menu where id = ?", [id]);
    ctx.body = {
        status: 200,
        data: res
    }
})
// 给角色分配菜单
router.post("/updateMenu", menuController.updateMenuByRoleId)
// 根据角色id查找菜单
router.get("/getMenuByRoleId", menuController.findAllByRoleId)

// 获取当前用户的菜单权限
router.post("/getMenu", menuController.getMenu)

module.exports = router;